package cn.itcast.hotel.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitMqConfig {
    public static final String HOTEL_DIRECT = "hotel.direct";
    public static final String HOTEL_INSERT_QUEUE = "hotel.insert.queue";
    public static final String HOTEL_DELETE_QUEUE = "hotel.delete.queue";
    public static final String HOTEL_INSERT_KEY = "hotel.insert.key";
    public static final String HOTEL_DELETE_KEY = "hotel.delete.key";

    /**
     * 创建交换机
     */
    @Bean
    public Exchange creatHotelDirectExchange() {
        return ExchangeBuilder.directExchange(HOTEL_DIRECT).durable(true).build();
    }

    /**
     * 创建队列 HOTEL_INSERT_QUEUE
     */
    @Bean
    public Queue creatHotelInsertQueue() {
        return QueueBuilder.durable(HOTEL_INSERT_QUEUE).build();
    }

    /**
     * 创建队列 HOTEL_DELETE_QUEUE
     */
    @Bean
    public Queue creatHotelDeleteQueue() {
        return QueueBuilder.durable(HOTEL_DELETE_QUEUE).build();
    }

    /**
     * 绑定交换机到队列 INSERT
     */
    @Bean
    public Binding bindInsertQueueToDirectExchange(
            @Qualifier("creatHotelDirectExchange") Exchange exchange,
            @Qualifier("creatHotelInsertQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(HOTEL_INSERT_KEY).noargs();
    }

    /**
     * 绑定交换机到队列 DELETE
     */
    @Bean
    public Binding bindDeleteQueueToDirectExchange(
            @Qualifier("creatHotelDirectExchange") Exchange exchange,
            @Qualifier("creatHotelDeleteQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(HOTEL_DELETE_KEY).noargs();
    }
}














